import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:get_storage/get_storage.dart';
import 'package:logger/logger.dart';
import 'package:sweetim/service/api_service.dart';
import 'package:sweetim/service/dao_service.dart';
import 'package:sweetim/service/member_service.dart';
import 'package:sweetim/service/socket_service.dart';
import 'package:sweetim/view/home_view.dart';
import 'package:sweetim/view/login_view.dart';

final GetStorage storage = GetStorage();

void main() async {
  await GetStorage.init();
  var memberService = MemberService();
  await initServices(memberService);
  runApp(GetMaterialApp(
    title: 'SweetIM',
    theme: ThemeData(
      primarySwatch: Colors.indigo,
      // appBarTheme: const AppBarTheme(
      //   foregroundColor: Colors.black87,
      //   backgroundColor: Colors.white,
      // ),
    ),
    enableLog: true,
    logWriterCallback: localLogWriter,
    initialRoute: "/home",
    getPages: [
      GetPage(
        name: "/home",
        page: () =>
            memberService.checkToken ? const HomeView() : const LoginView(),
      ),
      GetPage(
        name: "/login",
        page: () => const LoginView(),
      ),
    ],
  ));
}

initServices(MemberService memberService) {
  Get.put(DaoService(), permanent: true);
  Get.put(memberService, permanent: true);
  Get.put(SocketService(), permanent: true);
  Get.put(ApiService(), permanent: true);
  // Get.put(ApiService());
}

class MyImApp extends StatelessWidget {
  const MyImApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const HomeView();
  }
}

void localLogWriter(String text, {bool isError = false}) {
  var logger = Logger();
  if (isError) {
    logger.e(text);
    return;
  }
  logger.d(text);
}
